<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>线/面/圆可编辑</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <style>
    body,
    html,
    #container {
        overflow: hidden;
        width: 100%;
        height: 100%;
        margin: 0;
        font-family: "微软雅黑";
    }
    ul li {
        list-style: none;
    }
    .drawing-panel {
        z-index: 999;
        position: fixed;
        bottom: 3.5rem;
        margin-left: 3rem;
        padding: 1rem 1rem;
        border-radius: .25rem;
        background-color: #fff;
        box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
    }
    .btn {
        width: 150px;
        height: 30px;
        float: left;
        background-color: #fff;
        color: rgba(27, 142, 236, 1);
        font-size: 14px;
        border:1px solid rgba(27, 142, 236, 1);
        border-radius: 5px;
        margin: 0 5px;
        text-align: center;
        line-height: 30px;
    }
    .btn:hover {
        background-color: rgba(27, 142, 236, 0.8);
        color: #fff;
    }
    .btn-wrap {
        z-index: 999;
        position: fixed;
        bottom: 3.5rem;
        margin-left: 3rem;
        padding: 1rem 1rem;
        border-radius: .25rem;
        background-color: #fff;
        box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
    }
    
    .drawing-panel .bmap-polygon {
            background-position: -260px 0;
        }
    </style>
    <script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=xG0kdVrgdY6vpG5NvMs24nts07u3FtYV"></script>
</head>
<body>
    <div id="container"></div>
    <ul class="drawing-panel" style="z-index: 99;">
        <li class = "btn" onclick = "openEdit()">开启线/面/圆可编辑</li>
        <li class = "btn" onclick = "closeEdit()">关闭线/面/圆可编辑</li>
        <li class = "light btn" onclick = "addOverlay()">添加覆盖物</li>
        <li class = "night btn" onclick = "removeOverlay()">清除覆盖物</li>
        <li class = "night btn" onclick = "getLayerInformation()">获取绘制覆盖物信息</li>
        <li class = "night btn" onclick = "getduobianxing()">判断点是否在多边形中</li>
    </ul>
</body>
</html>

<script>
var map = new BMapGL.Map('container');
var point = new BMapGL.Point(116.404, 39.915);
map.centerAndZoom(point, 15);
map.enableScrollWheelZoom(true);

var marker = new BMapGL.Marker(point, {
    enableDragging: true
});
map.addOverlay(marker);

// 绘制面
var polygon = new BMapGL.Polygon([
    new BMapGL.Point(116.38565459850446, 39.92332693005358),
    new BMapGL.Point(116.38524299999993, 39.91306307719058),
    new BMapGL.Point(116.40822003291511, 39.91231456785572),
    new BMapGL.Point(116.40707020186234, 39.924378268268825),
], {
    strokeColor: 'blue',
    strokeWeight: 2,
    strokeOpacity: 0.5
});
map.addOverlay(polygon);


function openEdit() {
    polygon.enableEditing();
    marker.enableEditing();
}
function closeEdit() {
    polygon.disableEditing();
    marker.disableEditing();
}
// 添加覆盖物
function addOverlay() {
        map.addOverlay(polygon); // 增加多边形
    }
    // 清除覆盖物
    function removeOverlay() {
        map.clearOverlays();
    }
    // 获取覆盖物属性
function overlayStyle(e) {
    var p = e.target;
    if (p instanceof Bpolygon) {
        alert('该覆盖物是点，点的坐标为：' + p.getPosition().lng + ',' + p.getPosition().lat);
    } else if (p instanceof BMapGL.Circle) {
        alert('该覆盖物是圆，其半径为：' + p.getRadius() + '圆中心点坐标：' + p.getCenter().lng + ',' + p.getCenter().lat);
    } else {
        alert('无法获知该覆盖物类型');
    }
}
function getLayerInformation(){
    console.log(polygon.getPath());
    console.log(marker.getPosition());
}
function getduobianxing(){
    console.log(polygon.getPath());
    console.log(marker.getPosition());
    var point=marker.getPosition()
    var polygonPoints=polygon.getPath()
    if(isInPolygon(point, polygonPoints)){
        alert("该点在多边形内");
    }else{
        alert("该点不在多边形内");
    }
}
function isInPolygon(point, polygonPoints) {
  // point：待判断点的坐标
  // polygonPoints：多边形顶点坐标组成的数组
  let intersections = 0;
  const polygonLength = polygonPoints.length;
  for (let i = 0; i < polygonLength; i++) {
    const p1 = polygonPoints[i];
    const p2 = polygonPoints[(i + 1) % polygonLength];
    if (point.lat >= Math.min(p1.lat, p2.lat) && point.lat < Math.max(p1.lat, p2.lat)) {
      const x = (point.lat - p1.lat) * (p2.lng - p1.lng) / (p2.lat - p1.lat) + p1.lng;
      if (x === point.lng) {
        return true;
      }
      if (x > point.lng) {
        intersections++;
      }
    }
  }
  return intersections % 2 === 1;
}


</script>